package 动态规划;

import com.sun.tools.javac.Main;

import java.util.Scanner;

/**
 * @Author: biubiu0002
 * @Date: 2021-03-23 10:00
 **/
public class 最小路径和 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        int[][] grid = new int[m][n];
        System.out.println(new 最小路径和().minPathSum(grid));


    }

    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int i,j;
        int[][] dp = new int[m][n];
        dp[0][0]=grid[0][0];
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                if(i==0&&j>0){
                    dp[0][j]=dp[0][j-1]+grid[i][j];
                }
                else if(j==0&&i>0){
                    dp[i][0]=dp[i-1][0]+grid[i][j];
                }
                else if(i>0&&j>0){
                    dp[i][j]=grid[i][j]+Math.min(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[m-1][n-1];





    }
}
